﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V = Science.Mathematics.VectorCalculus;

namespace VectorCalculus5Ed.Chapter3.Section4
{
    public class Example11
    {
        public Example11()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
        public void Compute()
        {
            V.Function.ToLastType<double[], double> f
                = new V.Function.ToLastType<double[], double>(func);
            V.Function.ToLastType<double[], double> g
                = new V.Function.ToLastType<double[], double>(gf);
            double[] x = { 1.0, -1.0 };
            double l = 0.4;
            V.LagrangeMultiplierMethod obj
                = new V.LagrangeMultiplierMethod(f, g, x, l);
            obj.Compute();
            result += obj.CriticalPoint[0].ToString() + "\r\n";
            result += obj.CriticalPoint[1].ToString() + "\r\n";
            result += obj.Lambda.ToString() + "\r\n";
            result += func(obj.CriticalPoint).ToString() + "\r\n";

            V.HessianBordered hess = new V.HessianBordered(f, g, obj.CriticalPoint, obj.Lambda);
            hess.FindDeterminants();

            result += hess.Matrix[0, 0].ToString() + "\r\n";
            result += hess.Matrix[0, 1].ToString() + "\r\n";
            result += hess.Matrix[0, 2].ToString() + "\r\n";
            result += hess.Matrix[1, 0].ToString() + "\r\n";
            result += hess.Matrix[1, 1].ToString() + "\r\n";
            result += hess.Matrix[1, 2].ToString() + "\r\n";
            result += hess.Matrix[2, 0].ToString() + "\r\n";
            result += hess.Matrix[2, 1].ToString() + "\r\n";
            result += hess.Matrix[2, 2].ToString() + "\r\n";

            foreach(double kk in hess.Determinants)
                result += kk.ToString() + "\r\n";          
        }
        private double func(double[] x)
        {
            return (x[0] - x[1]) * (x[0] - x[1]);
        }
        private double gf(double[] x)
        {
            return x[0] * x[0] + x[1] * x[1] - 1.0;
        }
    }
}
/*
0.707106781186504
-0.707106781186591
1.99999999999905
2
0
-1.41421356237294
1.41421356237273
-1.41421356237294
-1.99999999857795
-2.00000000016251
1.41421356237273
-2.00000000016251
-1.99999999695707
-1.99999999999957
15.9999999917142
*/